import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:hello/enums/ExitStyle.dart';

const themeKey = "isDark"; // 主题设置储存key
DateTime _lastPressedAt; //上次点击时间

/* 退出应用 */
Future<bool> exitApp(BuildContext context, ExitStyle exitStyle){
  if(ExitStyle.doubleClick == exitStyle){
    return Future(exitAppWithDoubleClick);
  }else{
    return exitAppWithDialog(context);
  }
}

/* 以两次连续点击的方式退出应用 */
Future<bool> exitAppWithDoubleClick() {
    if (_lastPressedAt == null ||
        DateTime.now().difference(_lastPressedAt) > Duration(seconds: 2)) {
      //两次点击间隔超过1秒则重新计时
      _lastPressedAt = DateTime.now();
      return Future.value(false);
    }
    return Future.value(true);
}

/* 以弹框确认的方式退出应用 */
Future<bool> exitAppWithDialog(BuildContext context) async {
    return showDialog(
      context: context,
      builder: (context) => new AlertDialog(
        title: new Text('提示'),
        content: new Text('您确定要退出应用吗'),
        actions: <Widget>[
          new FlatButton(
            onPressed: () => Navigator.of(context).pop(false),
            child: new Text('不，再等等'),
          ),
          new FlatButton(
            onPressed: () => Navigator.of(context).pop(true),  //关闭对话框并返回true
            child: new Text('是的'),
          ),
        ],
      ),
    ) ?? false;
  }

// 保存主题设置
void cacheTheme(bool isDark) async {
  SharedPreferences sp = await SharedPreferences.getInstance();
  sp.setBool(themeKey, isDark);
}

// 读取主题设置
Future<bool> getCacheTheme() async {
  SharedPreferences sp = await SharedPreferences.getInstance();
  bool isDark = sp.getBool(themeKey);
  return isDark ??= false;
}

// 弹出对话框
Future<bool> showConfirmDialog(BuildContext context, String content, 
                {String title: "提示", String cancelBtnText: "取消",String confirmBtnText: "确定"}) {
  return showDialog<bool>(
    context: context,
    builder: (context) {
      return AlertDialog(
        title: Text(title),
        content: Text(content),
        actions: <Widget>[
          FlatButton(
            child: Text(cancelBtnText),
            onPressed: () => Navigator.of(context).pop(), // 关闭对话框
          ),
          FlatButton(
            child: Text(confirmBtnText),
            onPressed: () {
              //关闭对话框并返回true
              Navigator.of(context).pop(true);
            },
          ),
        ],
      );
    },
  );
}

// 加载中对话框
showLoadingDialog(BuildContext context, {String text: "正在加载，请稍后...", bool barrierDismissible: false}) {
  showDialog(
    context: context,
    barrierDismissible: barrierDismissible, //点击遮罩不关闭对话框
    builder: (context) {
      return UnconstrainedBox(
          constrainedAxis: Axis.vertical,
          child: SizedBox(
            width: 280,
            child: AlertDialog(
              content: Column(
                mainAxisSize: MainAxisSize.min,
                children: <Widget>[
                  CircularProgressIndicator(valueColor: AlwaysStoppedAnimation(Colors.blue)),
                  Padding(
                    padding: const EdgeInsets.only(top: 26.0),
                    child: Text(text),
                  )
                ],
              ),
            ),
          ),
        );
    },
  );
}